package com.xiu.travel.security.service.impl;

import com.xiu.travel.util.HttpClientUtil;
import com.alibaba.fastjson.JSON;
import com.xiu.travel.security.service.WeChatLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lijie
 * @date 2023/5/24 14:32
 * @description WeChatLoginServiceImpl
 */
@Service
@Slf4j
public class WeChatLoginServiceImpl implements WeChatLoginService
{
    @Value("${wechat.appid[1]}")
    private String APPID;
    @Value("${wechat.secret}")
    private String SECRET;

    @Value("${wechat.appid[2]}")
    private String ZJHAPPID;
    @Value("${wechat.zjh-secret}")
    private String ZJHSECRET;
    @Override
    public String getAccessToken()
    {
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token";
        Map<String, String> requestUrlParam = new HashMap<>();
        requestUrlParam.put("appid",APPID);
        requestUrlParam.put("secret",SECRET);
        requestUrlParam.put("grant_type","client_credential");
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doGet(requestUrl, requestUrlParam));
        log.info("微信AccessToken返回结果:{}",jsonObject);
        return jsonObject.get("access_token").toString();
    }
    @Override
    public String getZJHAccessToken()
    {
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token";
        Map<String, String> requestUrlParam = new HashMap<>();
        requestUrlParam.put("appid",ZJHAPPID);
        requestUrlParam.put("secret",ZJHSECRET);
        requestUrlParam.put("grant_type","client_credential");
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doGet(requestUrl, requestUrlParam));
        log.info("微信AccessToken返回结果:{}",jsonObject);
        return jsonObject.get("access_token").toString();
    }
}
